//@author A0105602X
#include "PaymentItem.h"

void PaymentItem::writefile(char* filename){
	ofstream outfile(filename,ios::app);
	Item::writefile(filename);
	int i;
	outfile << payer;                 //only need to retrieve payer information
	for (i = 0; i< maxnlength + 1 - payer.length(); i ++)
		outfile << ' ';
	outfile.flush();
	outfile.close();
}

bool PaymentItem::recordExtra(){
	cout << "Payer:\n";
	getline(cin,payer);
	while (payer.length() > maxnlength) {
		cout << "Length should not exceed " << maxnlength << ". Press Enter to exit.\n";
		getline(cin,payer);
		if (payer == "\n") return false; }
	return true;
}

bool PaymentItem::isexpense(){
	return false;
}

void PaymentItem::record_from_file(ifstream& infile, streampos address)
{
	Item::record_from_file(infile,address);
	payer = "";
	char last, c;
	int i;
	last = ' ';
	for (i = 0; i < maxnlength; i ++) {           //similar to retrieve name in Item class
		infile.get(c);
		if (! ((last == c) && (c == ' '))) payer = payer + c;
		last = c;
	}
	if (payer[payer.length() - 1] == ' ') payer.erase(payer.end() - 1);
	infile.get(c);
}

void PaymentItem::print(){
	Item::print();
	cout << '|' << payer << endl;
	cout << "-------------------------------------------------------------------------------\n";
}
bool PaymentItem::compare(Item& newSpec){
	if (newSpec.isexpense()) return false;
	if (!Item::compare(newSpec)) return false;
	if(payer == newSpec.getPayer()) return true;
	return false;
}
